﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using Jumbot.Common;
using JumbotCms.Common;
namespace JumbotCms.WebFile.Admin
{
    public partial class _javascriptajax : JumbotCms.API.AdminCenter
    {
        private string _operType = string.Empty;
        private string _response = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!CheckFormUrl())
            {
                Response.Write("提交有误");
                Response.End();
            }
            Admin_Load("0000", "json");
            this._operType = q("oper");
            switch (this._operType)
            {
                case "ajaxGetList":
                    ajaxGetList();
                    break;
                case "ajaxDel":
                    ajaxDel();
                    break;
                case "ajaxCopy":
                    ajaxCopy();
                    break;
                case "checkname":
                    ajaxCheckName();
                    break;
                case "ajaxCreateJavascript":
                    ajaxCreateJavascript();
                    break;
                default:
                    DefaultResponse();
                    break;
            }
            Response.Write(this._response);
        }

        private void DefaultResponse()
        {
            this._response = JsonResult(0, "未知操作");
        }
        private void ajaxCheckName()
        {
            if (q("id") == "0")
            {
                doh.Reset();
                doh.ConditionExpress = "code=@code";
                doh.AddConditionParameter("@code", q("txtCode"));
                if (doh.Exist("jcms_normal_javascript"))
                    this._response = JsonResult(0, "不可添加");
                else
                    this._response = JsonResult(1, "可以添加");
            }
            else
            {
                doh.Reset();
                doh.ConditionExpress = "code=@code and id<>" + q("id");
                doh.AddConditionParameter("@code", q("txtCode"));
                if (doh.Exist("jcms_normal_javascript"))
                    this._response = JsonResult(0, "不可修改");
                else
                    this._response = JsonResult(1, "可以修改");
            }
        }
        private void ajaxGetList()
        {
            doh.Reset();
            doh.SqlCmd = "Select [ID],[Title],[Code],[JumbotLiCode] FROM [jcms_normal_javascript] ORDER BY id desc";
            DataTable dt = doh.GetDataTable();
            this._response = "{result :\"1\",returnval :\"操作成功\"," + Jumbot.Common.JsonHelp.DtToSON(dt) + "}";
        }
        private void ajaxDel()
        {
            string sId = f("id");
            doh.Reset();
            doh.ConditionExpress = "id=@id";
            doh.AddConditionParameter("@id", sId);
            doh.Delete("jcms_normal_javascript");
            this._response = JsonResult(1, "成功删除");
        }
        private void ajaxCopy()
        {
            string sId = f("id");
            doh.Reset();
            doh.SqlCmd = "SELECT * FROM [jcms_normal_javascript] WHERE [Id]=" + sId;
            DataTable dtJavascript = doh.GetDataTable();
            if (dtJavascript.Rows.Count > 0)
            {
                doh.Reset();
                for (int i = 0; i < dtJavascript.Columns.Count; i++)
                {
                    if (dtJavascript.Columns[i].ColumnName.ToLower() == "id")
                        continue;
                    if (dtJavascript.Columns[i].ColumnName.ToLower() == "title")
                    {
                        string _title = dtJavascript.Rows[0][i].ToString().Split('_')[0];
                        doh.AddFieldItem(dtJavascript.Columns[i].ColumnName, _title + "_" + GetRandomNumberString(4, false));
                    }
                    else if (dtJavascript.Columns[i].ColumnName.ToLower() == "code")
                        doh.AddFieldItem(dtJavascript.Columns[i].ColumnName, GetRandomNumberString(64, false));
                    else
                        doh.AddFieldItem(dtJavascript.Columns[i].ColumnName, dtJavascript.Rows[0][i].ToString());

                }
                doh.Insert("jcms_normal_javascript");
            }
            dtJavascript.Clear();
            dtJavascript.Dispose();
            this._response = JsonResult(1, "成功克隆");
        }
        private void ajaxCreateJavascript()
        {
            string strXmlFile = HttpContext.Current.Server.MapPath("~/_data/config/javascript.config");
            Jumbot.DataOper.XmlControl XmlTool = new Jumbot.DataOper.XmlControl(strXmlFile);
            try
            {
                XmlTool.RemoveAll("Lis");
                XmlTool.Save();
                doh.Reset();
                doh.SqlCmd = "Select [Id],[Title],[Code],[ChannelId],[ClassId],[TitleLen],[JumbotLiCode],[SelectNumber] FROM [jcms_normal_javascript] ORDER BY id asc";
                DataTable dt = doh.GetDataTable();
                string _id = string.Empty;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    _id = dt.Rows[i]["Id"].ToString();
                    XmlTool = new Jumbot.DataOper.XmlControl(strXmlFile);
                    XmlTool.InsertNode("Lis", "Li", "ID", _id);
                    XmlTool.InsertElement("Lis/Li[ID=\"" + _id + "\"]", "Title", dt.Rows[i]["Title"].ToString(), false);
                    XmlTool.InsertElement("Lis/Li[ID=\"" + _id + "\"]", "Code", dt.Rows[i]["Code"].ToString(), true);
                    XmlTool.InsertElement("Lis/Li[ID=\"" + _id + "\"]", "ChannelId", dt.Rows[i]["ChannelId"].ToString(), false);
                    XmlTool.InsertElement("Lis/Li[ID=\"" + _id + "\"]", "ClassId", dt.Rows[i]["ClassId"].ToString(), false);
                    XmlTool.InsertElement("Lis/Li[ID=\"" + _id + "\"]", "SelectNumber", dt.Rows[i]["SelectNumber"].ToString(), false);
                    XmlTool.InsertElement("Lis/Li[ID=\"" + _id + "\"]", "JumbotLiCode", dt.Rows[i]["JumbotLiCode"].ToString(), false);
                    XmlTool.InsertElement("Lis/Li[ID=\"" + _id + "\"]", "TitleLen", dt.Rows[i]["TitleLen"].ToString(), false);
                    XmlTool.Save();
                }
                XmlTool.Dispose();
                this._response = JsonResult(1, "生成成功");
            }
            catch
            {
                this._response = JsonResult(0, "生成失败");
            }
        }
    }
}